package exception;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : ExceptionDemo7
 * @Author : 54061
 * @Date: 2022/2/11 19:13
 */
/*
    如何在方法内生成异常对象,让方法报异常:
        //前置会有判断条件
        1. 创建异常对象
        2. 暴露异常对象
            //编译时期异常在方法内暴露,方法自己需要抛出异常
 */
public class ExceptionDemo7 {
    public static void main(String[] args) throws AreUFoolishException {
        //setAge(200);

        //setName("鲁花花生油");

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //运行时期异常
    public static void setName(String name){
        if (name.length() >= 5){
            //报异常
            throw new AreUSBException("你给的名字不合法~");
        }
        System.out.println("您设置的名字是: " + name);
    }

    //编译时期异常
    public static void setAge(int age) throws AreUFoolishException {//抛出异常给调用者
        if (age < 0 || age > 150){
            //创建异常对象并暴露
            throw new AreUFoolishException("您给的年龄超出范围~");
        }

        System.out.println("您设置的年龄为: " + age);
    }
}
